package Action;


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import Form.SelectFrom;
import Model.user;
import Service.userService;
/**
 *  找回密码动作
 * @author MacBook
 *
 */
public class FindPasswordAction extends ActionSupport implements ModelDriven<SelectFrom>{
	private SelectFrom select = new SelectFrom();
	
	public SelectFrom getSelect() {
		return select;
	}

	public void setSelect(SelectFrom select) {
		this.select = select;
	}

	@Override
	public SelectFrom getModel() {
		// TODO Auto-generated method stub
		return select;
	}
	//action入口
	public String execute(){
		userService findMsg = new userService();
		user u  = findMsg.findUserMsg(select.getUsername(), select.getEmail(), select.getTel());
		ActionContext.getContext().getSession().put("updateUsername", select.getUsername());
		if(u == null)
			return ERROR;
		else
			return SUCCESS;
	}
	//ajax修改密码
	public void alterPassword(){
		userService updatePassword = new userService();
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = ActionContext.getContext().getSession().get("updateUsername").toString();
		System.out.println("=============="+username);
		if(username.equals(""))
		{
			HttpServletResponse response = ServletActionContext.getResponse();
			try {
				response.getWriter().print("false");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return;
		}
		String password = request.getParameter("updatePassword");
		updatePassword.alterUserPassword(username, password);

		
	}
	
}
